# -*- encoding: utf-8 -*-

DAILY_USER_REPORT = {
    'group_by': '_chn.keyword',
    'target_event_id': ['recharge', 'withdraw', 'tax', 'active', 'register', 'bind_phone'],
    'values': [
        {
            "value_id": "new_device",
            "target_value": "_aid.keyword",
            "value_name": "新增设备",
            "filter": [
                {'terms': {'_event_id': ['active']}},
                {'term': {'_device_active_days': 0}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_ip_device",
            "target_value": "_ip.keyword",
            "value_name": "首次登录的ip",
            "filter": [
                {'term': {'_event_id': 'register'}},
            ],
            "operation": "cardinality"
        },
        {
            "value_id": "new_user",
            "target_value": "_user_id",
            "value_name": "新增用户",
            "filter": [
                {'term': {'_event_id': 'register'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_device",
            "target_value": "_aid.keyword",
            "value_name": "新增注册设备",
            "filter": [
                {'term': {'_event_id': 'bind_phone'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_register_user",
            "target_value": "_user_id",
            "value_name": "新增注册用户",
            "filter": [
                {'term': {'_event_id': 'bind_phone'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_rate",
            "value_name": "当天注册率",
            "value_expr": "new_register_user/new_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "active_device",
            "target_value": "_aid.keyword",
            "value_name": "活跃设备",
            "filter": [
                {'term': {'_event_id': 'active'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "active_user",
            "target_value": "_user_id",
            "value_name": "活跃用户",
            "filter": [
                {'term': {'_event_id': 'active'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_amount",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
        },
        {
            "value_id": "recharge_user",
            "target_value": "_user_id",
            "value_name": "充值账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_device",
            "target_value": "_aid.keyword",
            "value_name": "充值设备",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_rate",
            "value_name": "付费率",
            "value_expr": "recharge_user/active_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "withdraw_amount",
            "target_value": "withdraw_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}},
            ],
        },
        {
            "value_id": "profit_amount",
            "target_value": ["recharge_price", "-", "withdraw_price"],
            "value_name": "营收金额",
            "filter": [
                {'terms': {'_event_id': ['recharge', 'withdraw']}},
            ],
        },
        {
            "value_id": "profit_rate",
            "value_name": "营收率",
            "value_expr": "profit_amount/recharge_amount",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },
        {
            "value_id": "tax",
            "target_value": "tax_tax",
            "value_name": "税收金额",
            "filter": [
                {'term': {'_event_id': 'tax'}},
            ]
        },
        {
            "value_id": "new_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "新增充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
        },
        {
            "value_id": "new_recharge_user",
            "target_value": "_user_id",
            "value_name": "新增充值账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_recharge_rate",
            "value_name": "新用户付费率",
            "value_expr": "new_recharge_user/new_register_user",
            "fmt": lambda v: float("%.2f" % (v * 100))
        },

    ]
}
